<?php
//    +----------------------------------------------------------------------
//    | Copyright (c) 2015-2035 http://www.hdphp.cn All rights reserved.
//    | Licensed ( http://www.hdphp.cn/licenses/ )
//    | Author: Jack <sophia2152@qq.com>
//    | 官方网址: http://www.hdphp.cn
//    | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//    | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//    +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\services\system\log;

use app\services\BaseServices;
use app\dao\system\log\SystemLogDao;
use app\services\system\admin\SystemAdminServices;
use app\services\system\SystemMenusServices;

/**
 * Class：SystemLogServices
 * 创建人：Jack
 * 创建时间：2023/3/21 7:48 PM
 * @package app\services\system\log
 * @mixin SystemLogDao
 */
class SystemLogServices extends BaseServices
{

    /**
     * SystemLogServices constructor.
     * @param SystemLogDao $dao
     */
    public function __construct(SystemLogDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 注释：记录访问日志
     * 创建人：Jack
     * 创建时间：2023/3/25 17:07
     * @param int $uid
     * @param string $true_name
     * @param string $method
     * @param string $rule
     * @param string $action
     * @param string|array $page
     * @param string $ip
     * @param array $data
     * @param string $type
     * @return bool
     */
    public function recordAdminLog(int $uid, string $true_name, string $method, string $rule, string $action, $page, string $ip, array $data = [], string $type = 'system')
    {
        $form_data = [];
        /** @var SystemMenusServices $service */
        $service = app()->make(SystemMenusServices::class);
        if (is_array($page)) {
            $page = $page[$action];
        }
        $form_data['uid'] = $uid;
        $form_data['true_name'] = $true_name;
        $form_data['method'] = $method;
        $form_data['path'] = $rule;
        $form_data['page'] = $page;
        $form_data['ip'] = $ip;
        $form_data['type'] = $type;
        $form_data['add_time'] = time();
        $newdata = '';
        foreach ($data as $key => &$value) {
            if (in_array($key, array('s', '_su'))) continue;
            if ($key == 'page' && $value == 0) continue;
            if (is_string($value)) {
                if (strlen($value) > 100) $value = '-MAX100-';
            } elseif (is_array($value)) {
                $value = '-array()-';
            }
            if ($key == 'password') $value = '******';
            $newdata .= $key . '=' . $value . "\r\n";
        }
        $form_data['data'] = $newdata;
        if ($this->dao->save($form_data)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 注释：获取系统日志列表
     * 创建人：Jack
     * 创建时间：2023/3/27 14:26
     * @param array $where
     * @param int $level
     * @return array
     */
    public function getLogList(array $where, int $level)
    {
        [$page, $limit] = $this->getPageValue();
        if (!$where['uid']) {
            /** @var SystemAdminServices $service */
            $service = app()->make(SystemAdminServices::class);
            $where['uid'] = $service->getUidIds($level);
        }
        $list = $this->dao->getLogList($where, $page, $limit);
        $count = $this->dao->count($where);

        return compact('list', 'count');
    }

}
